<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//SELECT DISTINCT tbl_pay.cheque_no , bank.bank_name, bank_branch.branch_name, "
//                . " credit_name AS receiver, status, tbl_pay.amount AS net , cheque_status, paid_status, pause_status"
//                . " FROM tbl_creditor, tbl_pet, bank, bank_branch,tbl_pay "
//                . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
//                . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
//                . " AND tbl_pet.pet_id = tbl_pay.pet_id "
//                . " AND tbl_pay.bank_id = bank.bank_id "
//                . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
//                . " ORDER BY cheque_no , bank_name, branch_name, credit_name";

//header("Content-Type:application/vnd.adobe.pdf");
require_once('./fpdf/tcpdf/tcpdf.php');
require_once('./fpdf/FPDI/fpdi.php');

// create new PDF document
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);

//$pdf->SetHeaderData("", "", "รายงานจัดทำเช็ค", "");
$header = '<table width="500" border="0" cellspacing="0" cellpadding="0"><thead>
  <tr>
    <td height="33" colspan="3">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . ')</td>
    <td colspan="1" align="right">วันที่ ' .  '</td>
  </tr>
   <tr >
    <td width="50" height="30" align="center"  >ลำดับที่</td>
    <td width="300" align="center" >ประเภทเงิน</td>
    <td width="150" align="center" >จำนวนเงิน</td>
  </tr>
 </thead></table><hr>';

$pdf->writeHTML($header, true, false, false, false, '');
//$pdf->SetHeaderData("", "",$xxx, "");
//$pdf->sethe

//function Header() {
//
//    $this->SetFont("thsarabun", "B", 16);
//    $header = '<table width="500" border="0" cellspacing="0" cellpadding="0"><thead>
//  <tr>
//    <td height="33" colspan="3">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . $this->var2 . ')</td>
//    <td colspan="1" align="right">วันที่ ' . $this->var1 . '</td>
//  </tr>
//   <tr >
//    <td width="50" height="30" align="center"  >ลำดับที่</td>
//    <td width="300" align="center" >ประเภทเงิน</td>
//    <td width="150" align="center" >จำนวนเงิน</td>
//  </tr>
// </thead></table><hr>';
//    $this->writeHTML('<br><div align="center">ใบสำคัญสรุปใบนำส่งเงิน</div>' . $header, true, false, false, false, '');
//}

// set header and footer fonts
//$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setHeaderFont(Array("thsarabunb", 'B', 20));

//$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
// ---------------------------------------------------------
// set font
//$pdf->SetFont('helvetica', 'B', 20);
//$pdf->SetFont('thsarabunb', 'B', 22);
// add a page
$pdf->AddPage();

//$pdf->Write(0, 'รายงานสมุดเงินสดจ่าย', '', 0, 'L', true, 0, false, false, 0);
//$pdf->SetFont('helvetica', '', 8);
// -----------------------------------------------------------------------------


$pdf->SetFont('thsarabunb', 'B', 16);

$tbl_header = '
<table cellspacing="0" cellpadding="3" border="1" width="100%">

';

require_once("includes/connect.php");
require_once("includes/function.php");

//$from = $_REQUEST["from"];
//$to = $_REQUEST["to"];
//$bank_name = $_REQUEST["bank_name"];
//$branch_name = $_REQUEST["branch_name"];
//$ex1_id = $_REQUEST["ex1_id"];


$db = new myDBS();
$db->selectDB("account");

$sql = "SELECT DISTINCT bank.bank_id, bank_branch.bank_branch_id , bank.bank_name, bank_branch.branch_name "
        . " FROM tbl_pet, bank, bank_branch,tbl_pay "
        . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
        . " AND tbl_pet.pet_id = tbl_pay.pet_id "
        . " AND tbl_pay.bank_id = bank.bank_id "
        . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id ";


//echo $sql;
$result = $db->runSQL($sql);

while ($rows = $db->fetch_object($result)) {
    $tbl_body = $tbl_body . '
        <tr>
            <td align="left" colspan="9">' . $rows->bank_name . '   ' . $rows->branch_name . '</td>
        </tr>

        ';


    $sql = "SELECT DISTINCT tbl_pay.cheque_no , tbl_pet.pet_id , credit_name AS receiver "
            . " , tbl_pay.amount, tbl_pet.net, panngan3.panngan3_id, ex2_id, ex3_id "
            . " FROM tbl_creditor, bank, bank_branch,tbl_pay, panngan3, tbl_pet"
            . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
            . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
            . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
            . " AND bank.bank_id = '$rows->bank_id' "
            . " AND bank_branch.bank_branch_id = '$rows->bank_branch_id' "
            . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
            . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
            . " AND tbl_pet.pet_id = tbl_pay.pet_id "
            . " AND tbl_pay.bank_id = bank.bank_id "
            . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
            . " ORDER BY tbl_pay.cheque_no";

//    $tbl_body = $tbl_body . '
//        <tr>
//            <td align="center">' . $sql . '</td>
//        </tr>
//    ';
    $result1 = $db->runSQL($sql);
    $tbl_body = $tbl_body . '
            <tr align="center" nobr="true">
                <td width="10%">เลขที่เช็ค</td>
                <td width="10%">เลขที่ฎีกา</td>
                <td width="10%">ผู้รับเงิน</td>
                <td width="15%">งาน</td>
                <td width="5%">หมวด</td>
                <td width="5%">ประเภท</td>
                <td width="15%">หน้าฎีกา</td>
                <td width="20%">ตามเช็ค</td>
                <td width="10%">ลายมือชื่อผู้รับเงิน</td>
            </tr>
            ';
    while ($rows1 = $db->fetch_object($result1)) {

        $tbl_body = $tbl_body . '
                    <tr nobr="true">
                        <td align="center">' . $rows1->cheque_no . '</td>
                        <td align="center">' . $rows1->pet_id . '</td>
                        <td align="center">' . $rows1->receiver1 . '</td>
                        <td align="center">' . $rows1->panngan3_id . '</td>
                        <td align="center">' . $rows1->ex2_id . '</td>
                        <td align="center">' . $rows1->ex3_id . '</td>
                        <td align="right">' . number_format($rows1->net, 2) . '</td>
                        <td align="right">' . number_format($rows1->amount, 2) . '</td>
                        <td align="right">&nbsp;</td>
                    </tr>
                    ';
    }
}

$tbl_footer = '</table>';

$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
//$pdf->writeHTML($sql);
$pdf->Output();
?>

